package NIO;

import lombok.Getter;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.FileChannel;

/**
 * @author 47868
 */
public class ChannelTest {
    public static void main(String[] args) {
        try {
            /*printFileDate();*/
            transferLargeFile();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    public static void printFileDate()throws Exception{
        RandomAccessFile randomAccessFile = new RandomAccessFile("file1.txt","rw");
        FileChannel fileChannel = randomAccessFile.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(2);
        while(fileChannel.read(buffer)!=-1){
            buffer.flip();
            while(buffer.hasRemaining()){
                System.out.print((char)buffer.get());
            }
            buffer.clear();
        }
        randomAccessFile.close();
    }

    public static void transferSimpleFile(){
        FileChannel source=null;
        FileChannel target=null;
        try {
             source= new RandomAccessFile("source.txt","rw").getChannel();
             target= new RandomAccessFile("target.txt","rw").getChannel();
             source.transferTo(0,source.size(),target);
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }  finally {
            try {
                if (source != null) {
                    source.close();
                }
            }
            catch (IOException e) {
                System.out.println(e.getMessage());
            }
            try {
                if (target != null) {
                    target.close();
                }
            }
            catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }
    public static void transferLargeFile(){
        FileChannel source=null;
        FileChannel target=null;
        try {
            source=new RandomAccessFile("source.txt","rw").getChannel();
            target=new RandomAccessFile("target.txt","rw").getChannel();
            long size=source.size();
            for(long left=size;left>0;){
                left-=source.transferTo((size-left),left,target);
            }
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
        finally {
            try {
                if (source != null) {
                    source.close();
                }
            }
            catch (IOException e) {
                System.out.println(e.getMessage());
            }
            try {
                if (source != null) {
                    target.close();
                }
            }
            catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }
}
